//
//  HomeViewController.m
//  LiquorChain
//
//  Created by 王冬 on 2018/12/19.
//  Copyright © 2018年 王冬. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()<FloatingBallHeaderDelegate,SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource >{
    LMJScrollTextView * _scrollTextView2;
    UITextField * _speedTF;
}
@property (nonatomic, strong) FloatingBallHeader *floatingBallHeader;
@property (nonatomic,strong) UIView *headView;
 @property (nonatomic,strong) UITableView *tableView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor =[UIColor whiteColor];
    
    UIView *headView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 500)];
    self.headView = headView;
    FloatingBallHeader *floatingBallHeader = [[FloatingBallHeader alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH , FloatingBallHeaderHeight)];
    floatingBallHeader.delegate = self;
    floatingBallHeader.dataList = @[@"1.2", @"0.05", @"1.88", @"10.55", @"20"];
    // , @"33", @"0.01", @"1.23",@"0.4",@"34"
    [headView addSubview:floatingBallHeader];
    self.floatingBallHeader = floatingBallHeader;
    
   // 向左，连续滚动
    [self addLabelWithFrame:CGRectMake(0, kNavStausHeight, 50, 30) text:@"公告:"];
    _scrollTextView2 = [[LMJScrollTextView alloc] initWithFrame:CGRectMake(50, kNavStausHeight, SCREEN_WIDTH - 50, 30) textScrollModel:LMJTextScrollContinuous direction:LMJTextScrollMoveLeft];
    _scrollTextView2.backgroundColor =[UIColor purpleColor];

    [floatingBallHeader addSubview:_scrollTextView2];
    
    [_scrollTextView2 startScrollWithText:@"最新公告:联想首发骁龙855新机没能刷屏,小米Play是“罪魁祸首" textColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:16]];
    
    NSArray *imageNames = @[@"h1",
                            @"h2",
                            ];
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 350, SCREEN_WIDTH, 150) shouldInfiniteLoop:YES imageNamesGroup:imageNames];
    cycleScrollView.delegate = self;
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    [headView addSubview:cycleScrollView];
    cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0,-20, SCREEN_WIDTH, SCREEN_HEIGHT)style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource= self;
    self.tableView.tableHeaderView = _headView;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"home"];
    [self.view addSubview:self.tableView];
}


#pragma mark - 系统代理
#pragma mark SDCycleScrollViewDelegate

// 点击图片代理方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    
    // 清理缓存
    [SDCycleScrollView clearImagesCache];
    
}

// 滚动到第几张图片的回调
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
    NSLog(@">>>>>> 滚动到第%ld张图", (long)index);
}
// 代理
- (void)floatingBallHeader:(FloatingBallHeader *)floatingBallHeader didPappaoAtIndex:(NSInteger)index isLastOne:(BOOL)isLastOne {
    NSLog(@"%@",floatingBallHeader.dataList[index]);
    if (isLastOne) {
        // 点了最后一个，刷新
        self.floatingBallHeader.dataList = @[@"2.2", @"0.15", @"8.88", @"5.55", @"2.22", @"3.33", @"0.11", @"1.23", @"6.66", @"7.89"];
    }
    
}
// 无限滚动
-(void)changeSpeed{
    if([_speedTF.text floatValue]) {
        CGFloat speed = [_speedTF.text floatValue];
        [_scrollTextView2 setMoveSpeed:speed];
    }
}
// 添加公告
-(void)addLabelWithFrame:(CGRect)frame text:(NSString *)text{
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor =[UIColor purpleColor];
    label.text      = text;
    label.textColor = [UIColor whiteColor];
    label.font      = [UIFont boldSystemFontOfSize:16];
    [self.headView addSubview:label];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.floatingBallHeader resetAnimation];
}
#pragma mark UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headview =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    headview .backgroundColor =[UIColor redColor];
    UILabel *titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 50)];
    titleLabel.text = @"排行榜";
    [headview addSubview:titleLabel];
    return headview;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"home"];
    cell.textLabel.text = @"123";
    return cell;
}
@end
